package com.wkbb.sports.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.wkbb.common.handler.JsonUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

//房间数据
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class StudentOnlineRoom implements Serializable {
    @ApiModelProperty(value = "编号")
    private Long id;
    @ApiModelProperty(value = "状态 0：关闭 1：开启 2比赛中")
    private Integer state = 1;
    @ApiModelProperty(value = "玩家人数")
    private Integer playerNum;
    @ApiModelProperty(value = "学生列表")
    private List<StudentOnlineRoomStu> stuList = new ArrayList<>();
    @ApiModelProperty(value = "观战列表")
    private List<StudentOnlineRoomStu> watchStuList = new ArrayList<>();

    @ApiModelProperty(value = "房主ID")
    public Long getRoomOwnersId() {
        return stuList.isEmpty() ? 0 : stuList.get(0).getStudentId();
    }

    //学生退出房间
    public void stuExit(StudentOnlineHall onlineHall) {
        onlineHall.setRoomId(null);
        onlineHall.setRoomState(null);
        if (onlineHall.getState() > 1) onlineHall.setState(1);
        Optional<StudentOnlineRoomStu> optional = stuList.stream().filter(item -> item.getStudentId().equals(onlineHall.getStudentId())).findFirst();
        if (optional.isPresent()){
            StudentOnlineRoomStu stu = optional.get();
            stuList.remove(stu);
        }else{
            optional = watchStuList.stream().filter(item -> item.getStudentId().equals(onlineHall.getStudentId())).findFirst();
            if (optional.isPresent()){
                StudentOnlineRoomStu stu = optional.get();
                stuList.remove(stu);
            }
        }
    }

    public StudentOnlineRoomStu getStu(StudentOnlineHall onlineHall){
//        System.out.println("list:"+JsonUtil.objectToJson(list));
        Optional<StudentOnlineRoomStu> optional = watchStuList.stream().filter(item -> item.getStudentId().equals(onlineHall.getStudentId())).findFirst();
        if (optional.isPresent()) return optional.get();
        else{
            optional = stuList.stream().filter(item -> item.getStudentId().equals(onlineHall.getStudentId())).findFirst();
            return optional.orElse(null);
        }
    }

    public void removeStu(StudentOnlineHall onlineHall){
        boolean b =  stuList.removeIf(item -> item.getStudentId().equals(onlineHall.getStudentId()));
        if (!b) watchStuList.removeIf(item -> item.getStudentId().equals(onlineHall.getStudentId()));
    }

    public static void main(String[] args) {
        StudentOnlineRoom room = JsonUtil.jsonToClass("{\"id\": 9,\"state\": 2,\"playerNum\": 2,\"stuList\": [{\"roomId\": 9,\"studentId\": 1272201,\"studentName\": \"开发测试\",\"headImgUrl\": \"https://picshcostest.wkbaobao.com/miniprogram/img/student_male.png\",\"avgAMinuteCount\": 379,\"count\": 0,\"state\": 2,\"robot\": false},{\"roomId\": 9,\"studentId\": 3,\"studentName\": \"陈红兰\",\"avgAMinuteCount\": 360,\"count\": 0,\"state\": 2,\"robot\": false}],\"watchStuList\": [],\"full\": false,\"roomOwnersId\": 1272201,\"ready\": false}",StudentOnlineRoom.class);
        StudentOnlineHall hall = JsonUtil.jsonToClass("{\"id\":1,\"studentId\":3,\"studentName\":\"陈红兰\",\"avgAMinuteCount\":360,\"longitude\":116.459778,\"latitude\":39.918789,\"state\":2,\"roomId\":9,\"roomState\":2}",StudentOnlineHall.class);
        System.out.println(JsonUtil.objectToJson(room.getStu(hall)));
    }

    //返回房间内是否为空
    public boolean stuIsEmpty() {
        return stuList.isEmpty() && watchStuList.isEmpty();
    }

    //返回房间是否人数已满
    public boolean isFull() {
        return stuList.isEmpty() && watchStuList.isEmpty();
    }

    //所有人准备好
    public boolean isReady(){
        for (int i = 1;i < stuList.size();i++){
            if (stuList.get(i).getState() != 1) return false;
        }
        return true;
    }

}